package com.jade.explorer.ui.exp;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import com.jade.logger.Logger;


public class ExportProject implements IExportTarget {

	private Composite _layer = null;
	private String _title = "Project";
	private String _localPath = "";
	private Button _b = null;
	private String _expPath = "";
	private boolean _over = false;
	private boolean _result = false;
	
	@Override
	public void addTargetItem(Tree t) {
		TreeItem ti = new TreeItem(t, 0);
		ti.setText(_title);
		ti.setImage(JadeSharedImages.getImage(JadeSharedImages.IMG_FOLDER_STAND));
	}

	@Override
	public void setLocalPath(String localPath) {
		_localPath = localPath;
	}

	@Override
	public boolean getResult() {
		return _result;
	}

	@Override
	public void run() {
		
		if (ready()){
			if (_over)
				_result = FileSystem.copyFilderWithOverwritingExistingFiles(_localPath, _expPath);
			else
				_result = FileSystem.copyFilderWithNoOverwritingExistingFiles(_localPath, _expPath);
		}

	}

	@Override
	public boolean ready() {
		if (_expPath != "")
			return true;
		return false;
	}

	@Override
	public void setVisible(boolean b) {
		_layer.setVisible(b);
	}

	@Override
	public void setEnabled(boolean b) {
		_layer.setEnabled(b);
	}

	@Override
	public String getTitle() {
		return _title;
	}

	@Override
	public Composite createLayer(Composite parent) {
		
		_layer = new Composite(parent, SWT.NONE);
		_layer.setBounds(11, 13, 434, 317);
		_layer.setBounds(0, 44, 444, 327);
		
		final Text pathText = new Text(_layer, SWT.BORDER);
		pathText.setEditable(false);
		pathText.setBounds(10, 114, 251, 21);
		
		final Shell shell = parent.getShell();
		Button pathButton = new Button(_layer, SWT.NONE);
		pathButton.setBounds(267, 112, 167, 25);
		pathButton.setText("Choose path");
		pathButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				DirectoryDialog dialog = new DirectoryDialog(shell);
				String exportPath = dialog.open(); 
				
				if (exportPath != null){
					pathText.setText(exportPath);
					_expPath = exportPath;
					_b.setEnabled(true);
				}
			}
		});
		
		Button overwriteButton = new Button(_layer, SWT.CHECK);
		overwriteButton.setBounds(10, 140, 424, 16);
		overwriteButton.setText("Overwrite existing files");
		overwriteButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_over) _over  = false;
				else _over = true;
				
			}
		});
		
		return _layer;
		
	}

	@Override
	public void buttonControl(Button b) {
		_b = b;
	}
	
	@Override
	public void clearData(){
		_expPath = "";
		_localPath = "";
	}

}
